package bank.soap;

import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

import bank.Account;
import bank.Bank;
import bank.InactiveException;
import bank.OverdrawException;
import bank.soap.client.BankServiceImplService;
import bank.soap.client.BankServiceImpl;
import bank.soap.client.IOException_Exception;
import bank.soap.client.IllegalArgumentException_Exception;
import bank.soap.client.InactiveException_Exception;
import bank.soap.client.OverdrawException_Exception;

public class SOAPBank implements Bank {
	private BankServiceImpl port;

	public SOAPBank() {
		BankServiceImplService service = new BankServiceImplService();
		port = service.getBankServiceImplPort();
	}
	
	public String createAccount(String owner) throws IOException {
		try {
			return port.createAccount(owner);
		} catch (IOException_Exception e) {
			e.printStackTrace();
		}
		return owner;
	}
	
	public boolean removeAccount(String number) throws IOException {
		try {
			return port.removeAccount(number);
		} catch (IOException_Exception e) {
			e.printStackTrace();
		}
		return false;
	}
	
	public Set<String> getAccountNumbers() throws IOException {
		Set<String> set = new HashSet<String>();
		for (Object s : port.getAccountNumbers()) {
			set.add((String) s);
		}

		return set;
	}

	public Account getAccount(String number) throws IOException {
		return new SOAPAccount(port, number);
	}

	public void transfer(Account a, Account b, double amount) throws IOException, IllegalArgumentException, OverdrawException, InactiveException {
		try {
			port.transfer(a.getNumber(), b.getNumber(), amount);
		} catch (IOException_Exception e) {
			e.printStackTrace();
		} catch (IllegalArgumentException_Exception e) {
			e.printStackTrace();
		} catch (InactiveException_Exception e) {
			e.printStackTrace();
		} catch (OverdrawException_Exception e) {
			e.printStackTrace();
		}
	}
}
